﻿using BaoVeThanhDia.MapFolder;
using BaoVeThanhDia.Sound;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Devices;

namespace BaoVeThanhDia.GamePlay.Monsters
{
    public class Monster
    {
        private int Type;// loại lính
        private float HP;// lượng máu chống chịu
        private float curHP;//lượng máu còn lại
        private int Value;// giá trị khi chết
        private float PhysicDef;// phòng thủ sát thương vật lý
        private float MagicDef;//phòng thủ sát thương phép thuật
        private int Dame;// máu thành bị trừ khi quái vật đến được đích
        private float Speed;// tốc độ di chuyển
        private Vector2 curPo;// vị trí vẽ
        private Vector2 Index;// tọa độ trong map
        private string Status;// gồm died,dying,walking,harming các trạng thái di chuyển của quái vật
        private float scale;
        private Texture2D Image;
        private Texture2D ImageHP, ImageHPbar, ImageNe;
        private string Ablility;
        private int countDraw, Row, Col;
        private Vector2 sizeTotal, IndexRe;
        private string Huong;
        private bool isEnable;
        private bool isTangHinh, isNe;
        private int countNeAndTangHinh;
        private Vector2 DolechAnh;
        private List<MonsterEffect> effects;
        bool isCanXe;
        int counttoHarming;
        bool isRevival;
        CustomSoundEffect die, reveal, phahoai;
        VibrateController testVibrateController; // rung màn hình
        public Monster(int type, Vector2 index, float speed, float hp, string huong = "Right", bool isValue = true)
        {
            testVibrateController = VibrateController.Default;
            die = new CustomSoundEffect("sounds/effect/Monster/Die");
            reveal = new CustomSoundEffect("sounds/effect/Monster/Reveal");
            phahoai = new CustomSoundEffect("sounds/effect/Monster/Pha_Hoai");
            counttoHarming = 0;
            effects = new List<MonsterEffect>();
            countNeAndTangHinh = 0;
            HP = hp;
            isEnable = true;
            curHP = hp;
            Type = type;
            countDraw = 0;
            Speed = speed;
            Huong = huong;
            Status = "walking";
            isTangHinh = false;
            DolechAnh = Vector2.Zero;
            setAsset(isValue);
            setIndex(index);
            if (Type == 9) AddListEffect("Heal", 1, 1);
            isCanXe = false;
            isRevival = false;
            setAmination();
        }
        #region nơi get, set các thuộc tính
        public void setIndex(Vector2 index)
        {
            Index = index;
            IndexRe = index;
            int tempSize = 128;
            curPo = new Vector2(tempSize * index.Y, tempSize * index.X);
        }
        public string getStatus() { return Status; }
        public Vector2 getPosition() { return curPo; }
        public bool getIsEnable() { return isEnable; }
        private void setAsset(bool isvalue)
        {
            switch (Type)
            {
                case 1:
                    {
                        Value = 15;
                        PhysicDef = 0;
                        MagicDef = 0;
                        Dame = 1;
                        Ablility = "";
                        DolechAnh = new Vector2(0, -50);
                    }
                    break;
                case 2:
                    {
                        Value = 20;
                        PhysicDef = 10;
                        MagicDef = 0;
                        Dame = 1;
                        Ablility = "";
                        Speed *= 0.8f;
                        DolechAnh = new Vector2(20, -60);
                    }
                    break;
                case 3:
                    {
                        Value = 25;
                        PhysicDef = 7;
                        MagicDef = 7;
                        Dame = 1;
                        Ablility = "";
                        DolechAnh = new Vector2(15, -35);
                    }
                    break;
                case 4:
                    {
                        Value = 30;
                        PhysicDef = 7;
                        MagicDef = 10;
                        Dame = 1;
                        Ablility = "HS30";
                        Speed *= 2;
                        DolechAnh = new Vector2(0, -60);
                    }
                    break;
                case 5:
                    {
                        Value = 35;
                        PhysicDef = 15;
                        MagicDef = 15;
                        Dame = 1;
                        Ablility = "De1";
                        Speed *= 1.2f;
                        DolechAnh = new Vector2(0, -60);
                    }
                    break;
                case 6:
                    {
                        Value = 40;
                        PhysicDef = 15;
                        MagicDef = 5;
                        Dame = 2;
                        Ablility = "Ne";
                        DolechAnh = new Vector2(0, -40);
                    }
                    break;
                case 7:
                    {
                        Value = 50;
                        PhysicDef = 15;
                        MagicDef = 15;
                        Dame = 2;
                        Ablility = "";
                        Speed *= 2.5f;
                        DolechAnh = new Vector2(0, -60);
                    }
                    break;
                case 8:
                    {
                        Value = 60;
                        PhysicDef = 10;
                        MagicDef = 20;
                        Dame = 2;
                        Ablility = "Ne";
                        Speed *= 1.7f;
                        DolechAnh = new Vector2(0, -60);
                    }
                    break;
                case 9:
                    {
                        Value = 70;
                        PhysicDef = 12;
                        MagicDef = 25;
                        Dame = 2;
                        Ablility = "HoiMau";
                        Speed *= 1.4f;
                        DolechAnh = new Vector2(0, -60);
                    }
                    break;
                case 10:
                    {
                        Value = 80;
                        PhysicDef = 20;
                        MagicDef = 17;
                        Dame = 2;
                        Ablility = "Hoi25";
                        Speed *= 1.3f;
                        DolechAnh = new Vector2(0, -60);
                    }
                    break;
                case 11:
                    {
                        Value = 60;
                        PhysicDef = 15;
                        MagicDef = 20;
                        Dame = 3;
                        Ablility = "TangHinh";
                        Speed *= 1.5f;
                        DolechAnh = new Vector2(0, -60);
                    }
                    break;
                case 12:
                    {
                        Value = 100;
                        PhysicDef = 20;
                        MagicDef = 15;
                        Dame = 3;
                        Ablility = "Ne";
                        Speed *= 1.2f;
                        DolechAnh = new Vector2(0, -60);
                    }
                    break;
                case 13:
                    {
                        Value = 100;
                        PhysicDef = 20;
                        MagicDef = 20;
                        Dame = 3;
                        Ablility = "Khang";
                        Speed *= 1.2f;
                        DolechAnh = new Vector2(0, -60);
                    }
                    break;
                case 14:
                    {
                        Value = 120;
                        PhysicDef = 30;
                        MagicDef = 30;
                        Dame = 3;
                        Ablility = "HS50";
                        Speed *= 1.5f;
                        DolechAnh = new Vector2(0, -60);
                    }
                    break;
                default:
                    {
                        Value = 150;
                        PhysicDef = 50;
                        MagicDef = 50;
                        Dame = 3;
                        Ablility = "De6";
                        Speed *= 1.5f;
                        DolechAnh = new Vector2(0, -60);
                    }
                    break;
            }
            if (!isvalue) Value = 0;
        }
        public float getCurHP() { return curHP; }
        public void setAmination()
        {
            /* sizetotal là kích thước ảnh thật
             * col,row là hàng, cột các ảnh trong ảnh gốc để vẽ
             */
            scale = 150 / (sizeTotal.X / Col);
            switch (Type)
            {
                case 1:
                    {
                        #region set các chỉ số
                        scale = 110 / (sizeTotal.X / Col);
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(323, 64f);
                            Col = 5;
                            Row = 1;
                        }
                        else
                        {
                            sizeTotal = new Vector2(387, 65);
                            Col = 6;
                            Row = 1;
                        }
                        #endregion
                        break;
                    }
                case 2:
                    {
                        #region set các chỉ số
                        scale = 160 / (sizeTotal.X / Col);
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(294, 196);
                            Col = 3;
                            Row = 2;
                        }
                        else
                        {
                            sizeTotal = new Vector2(392, 196);
                            Col = 4;
                            Row = 2;
                        }
                        #endregion
                        break;
                    }
                case 3:
                    {
                        #region set các chỉ số
                        scale = 100 / (sizeTotal.X / Col);
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(171, 114);
                            Col = 3;
                            Row = 2;
                        }
                        else
                        {
                            sizeTotal = new Vector2(347, 69);
                            Col = 5;
                            Row = 1;
                        }
                        #endregion
                        break;
                    }
                case 4:
                    {
                        #region set các chỉ số
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(420, 280);
                            Col = 3;
                            Row = 2;
                        }
                        else
                        {
                            sizeTotal = new Vector2(700, 140);
                            Col = 5;
                            Row = 1;
                        }
                        #endregion
                        break;
                    }
                case 5:
                    {
                        #region set các chỉ số
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(300, 200);
                            Col = 3;
                            Row = 2;
                        }
                        else if (Status.Equals("dying"))
                        {
                            sizeTotal = new Vector2(500, 100);
                            Col = 5;
                            Row = 1;
                        }
                        else
                        {
                            sizeTotal = new Vector2(300, 200);
                            Col = 3;
                            Row = 2;
                        }
                        #endregion
                        break;
                    }
                case 6:
                    {
                        #region set các chỉ số
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(319, 170);
                            Col = 3;
                            Row = 2;
                        }
                        else
                        {
                            scale = 180 / (sizeTotal.X / Col);
                            sizeTotal = new Vector2(478, 144);
                            Col = 4;
                            Row = 2;
                        }
                        #endregion
                        break;
                    }
                case 7:
                    {
                        #region set các chỉ số
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(400, 200);
                            Col = 4;
                            Row = 2;
                        }
                        else
                        {
                            scale = 205 / (sizeTotal.X / Col);
                            sizeTotal = new Vector2(417, 212);
                            Col = 3;
                            Row = 2;
                        }
                        #endregion
                        break;
                    }
                case 8:
                    {
                        #region set các chỉ số
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(240, 160);
                            Col = 3;
                            Row = 2;
                        }
                        else
                        {
                            sizeTotal = new Vector2(400, 80);
                            Col = 5;
                            Row = 1;
                        }
                        #endregion
                        break;
                    }
                case 9:
                    {
                        #region set các chỉ số
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(254, 169);
                            Col = 3;
                            Row = 2;
                        }
                        else
                        {
                            sizeTotal = new Vector2(424, 85);
                            Col = 5;
                            Row = 1;
                        }
                        #endregion
                        break;
                    }
                case 10:
                    {
                        #region set các chỉ số
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(255, 170);
                            Col = 3;
                            Row = 2;
                        }
                        else if (Status.Equals("dying"))
                        {
                            sizeTotal = new Vector2(255, 85);
                            Col = 3;
                            Row = 1;
                        }
                        else
                        {
                            sizeTotal = new Vector2(255, 170);
                            Col = 3;
                            Row = 2;
                        }
                        #endregion
                        break;
                    }
                case 11:
                    {
                        #region set các chỉ số
                        if (Status.Equals("walking"))
                        {
                            scale = 125 / (sizeTotal.X / Col);
                            sizeTotal = new Vector2(210, 162);
                            Col = 3;
                            Row = 2;
                        }
                        else
                        {
                            sizeTotal = new Vector2(263, 156);
                            Col = 3;
                            Row = 2;
                        }
                        #endregion
                        break;
                    }
                case 12:
                    {
                        #region set các chỉ số
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(318, 212);
                            Col = 3;
                            Row = 2;
                        }
                        else
                        {
                            scale = 170 / (sizeTotal.X / Col);
                            sizeTotal = new Vector2(395, 263);
                            Col = 3;
                            Row = 2;
                        }
                        #endregion
                        break;
                    }
                case 13:
                    {
                        #region set các chỉ số
                        scale = 130 / (sizeTotal.X / Col);
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(400, 80);
                            Col = 5;
                            Row = 1;
                        }
                        else
                        {
                            sizeTotal = new Vector2(400, 80);
                            Col = 5;
                            Row = 1;
                        }
                        #endregion
                        break;
                    }
                case 14:
                    {
                        #region set các chỉ số
                        scale = 170 / (sizeTotal.X / Col);
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(532, 329);
                            Col = 3;
                            Row = 2;
                        }
                        else
                        {
                            sizeTotal = new Vector2(709, 165);
                            Col = 4;
                            Row = 1;
                        }
                        #endregion
                        break;
                    }
                case 15:
                    {
                        #region set các chỉ số
                        scale = 160 / (sizeTotal.X / Col);
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(900, 150);
                            Col = 6;
                            Row = 1;
                        }
                        else if (Status.Equals("dying"))
                        {
                            sizeTotal = new Vector2(900, 150);
                            Col = 6;
                            Row = 1;
                        }
                        else
                        {
                            sizeTotal = new Vector2(1050, 150);
                            Col = 7;
                            Row = 1;
                        }
                        #endregion
                        break;
                    }
                default:
                    {
                        #region set các chỉ số
                        if (Status.Equals("walking"))
                        {
                            sizeTotal = new Vector2(400, 200);
                            Col = 4;
                            Row = 2;
                        }
                        else
                        {
                            sizeTotal = new Vector2(417, 212);
                            Col = 3;
                            Row = 2;
                        }
                        #endregion
                        break;
                    }
            }
        }
        #endregion

        public void Load(ContentManager content)
        {
            if (ImageHP == null || ImageHP.IsDisposed)
            {
                ImageHP = content.Load<Texture2D>("images/monsters/HP");
                ImageHPbar = content.Load<Texture2D>("images/monsters/HP_bar");
                ImageNe = content.Load<Texture2D>("images/monsters/miss");
                phahoai.Load(content);
                die.Load(content);
                reveal.Load(content);
            }
            if (Status.Equals("walking"))
            {
                Image = content.Load<Texture2D>("images/monsters/" + Type.ToString() + "_walk");
            }
            else if (Status.Equals("dying"))
            {
                Image = content.Load<Texture2D>("images/monsters/" + Type.ToString() + "_die");
            }
            else if (Status.Equals("harming"))
            {
                Image = content.Load<Texture2D>("images/monsters/" + Type.ToString() + "_dame");
            }
            setAmination();
        }
        public void Draw(SpriteBatch sprite, ContentManager cont)
        {
            Load(cont);
            #region kiểm tra xem có đang ở trạng thái né hay Tàng hình không?
            if (Ablility.Equals("TangHinh") && countNeAndTangHinh / 10 >= 5)
            {
                isTangHinh = true;
            }
            else isTangHinh = false;
            countNeAndTangHinh++;
            if (countNeAndTangHinh >= 100) countNeAndTangHinh = 0;
            #endregion
            #region vẽ quái vật
            if (Image.IsDisposed)
            {
            }
            else
            {
                miniDraw(sprite, cont, sizeTotal, Row, Col);
            }
            #endregion

            #region vẽ thanh máu
            if (ImageHPbar.IsDisposed || ImageHP.IsDisposed)
            {
            }
            else
            {
                float percent = curHP * 100 / HP;
                if (percent > 0)
                {
                    sprite.Draw(ImageHPbar, (curPo + new Vector2(13, -35)) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, 101, 8), Color.White, 0f, Vector2.Zero, GamePage.Scale, SpriteEffects.None, 0f);
                    sprite.Draw(ImageHP, (curPo + new Vector2(14, -35)) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, (int)percent, 8), Color.White, 0f, Vector2.Zero, GamePage.Scale, SpriteEffects.None, 0f);
                }
            }
            #endregion
            #region vẽ chữ né nếu quái vật vẫn còn sống và trong trạng thái né
            if (ImageNe.IsDisposed)
            {
            }
            else
            {
                if (isNe && !Status.Equals("dying"))
                {
                    sprite.Draw(ImageNe, (curPo + new Vector2(14, -35)) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, 92, 76), Color.White, 0f, Vector2.Zero, 0.7f * GamePage.Scale, SpriteEffects.None, 0f);
                }
            }
            #endregion
            #region vẽ các hiệu ứng đang gánh chịu
            foreach (MonsterEffect effect in effects)
            {
                if (effect.getImage() == null || effect.getImage().IsDisposed)
                {
                    effect.Load(cont);
                }
                effect.Draw(sprite, curPo, cont);
            }
            #endregion
        }
        public void Walk()
        {
            if (Status.Equals("walking") || Status.Equals("harming"))
            {
                #region thực hiện đẻ
                if (Status.Equals("harming") && !isCanXe) //nếu có đẻ thì phải đẻ
                {
                    if (Ablility.Equals("De1"))
                    {
                        GamePage.monsters.Add(new Monster(1, Index, Speed / 3 - 1, HP / 3, Huong, false));
                        GamePage.monsters.Add(new Monster(1, Index, Speed / 3, HP / 3, Huong, false));
                        GamePage.monsters.Add(new Monster(1, Index, Speed / 3 + 1, HP / 3, Huong, false));
                    }
                    else if (Ablility.Equals("De6"))
                    {
                        GamePage.monsters.Add(new Monster(6, Index, Speed / 3 - 1, HP / 3, Huong, false));
                        GamePage.monsters.Add(new Monster(6, Index, Speed / 3, HP / 3, Huong, false));
                        GamePage.monsters.Add(new Monster(6, Index, Speed / 3 + 1, HP / 3, Huong, false));
                    }
                    else
                    {
                        curHP += HP / 4;
                        if (curHP > HP)
                        {
                            curHP = HP;
                        }
                    }
                    isCanXe = true;
                }
                #endregion
                #region loại bỏ các trạng thái hết tác dụng
                for (int i = 0; i < effects.Count; i++)
                {
                    if (!effects[i].getActive()) effects.RemoveAt(i);
                }
                #endregion
                int giamSpeed = 0;
                #region hiệu ứng chảy máu theo thời gian
                if (isTonTaiHieuUng("Heal") && curHP > 0 && !isTonTaiHieuUng("Stun") && !isTonTaiHieuUng("Frozen"))
                {
                    curHP += HP / 1000;
                    if (curHP > HP)
                    {
                        curHP = HP;
                    }
                }
                if (isTonTaiHieuUng("Poison") && curHP > 0)
                {
                    curHP -= getCountEffect("Poison");
                    if (curHP <= 0)
                    {
                        Status = "dying";
                        if ((Ablility.Equals("HS30") || Ablility.Equals("HS50")) && !isRevival)
                        {
                            isRevival = true;
                            reveal.Play();
                            AddListEffect("Revival", Ablility.Equals("HS30") ? 30 : 50, 1);
                        }
                        else
                        {
                            die.Play();
                            GamePage.Money += Value;
                            GamePage.Score += Value * 2;
                            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[1])
                            {
                                GamePage.Money += Value / 20;
                            }
                        }
                        countDraw = 0;
                    }
                }
                if (isTonTaiHieuUng("Slow"))
                {
                    giamSpeed = getCountEffect("Slow");
                }
                #endregion
                if (!isTonTaiHieuUng("Frozen") && !isTonTaiHieuUng("Stun"))
                {
                    #region không bị đóng băng với choáng thì mới di chuyển
                    #region tính vị trí hiện tại
                    Index.Y = (int)(curPo.X) / 128;
                    Index.X = (int)(curPo.Y) / 128;
                    if (Index.X < 0) Index.X = 0;
                    if (Index.Y < 0) Index.Y = 0;
                    if (Index.X >= Map.SizeOfMatrix.X) Index.X = Map.SizeOfMatrix.X - 1;
                    if (Index.Y >= Map.SizeOfMatrix.Y) Index.Y = Map.SizeOfMatrix.Y - 1;
                    #endregion

                    #region xác định rẽ
                    {
                        switch (Huong)
                        {
                            case "Top":
                                #region
                                if (Math.Abs(curPo.Y - Index.X * 128) <= 10)
                                {
                                    if (!GamePage.map.XacDinhRe(Index, Huong).Equals(Huong) && Index != IndexRe)
                                    {
                                        Huong = GamePage.map.XacDinhRe(Index, Huong);
                                        IndexRe = Index;
                                    }
                                    else
                                        if ((Index.X == 0) || Map.matrix[(int)Index.X - 1, (int)Index.Y] != 0)
                                        {
                                            if (((int)Index.Y < Map.SizeOfMatrix.Y - 1) && (Map.matrix[(int)Index.X, (int)Index.Y + 1] == 0))
                                            {
                                                Huong = "Right";
                                            }
                                            else if (((int)Index.Y > 0) && (Map.matrix[(int)Index.X, (int)Index.Y - 1] == 0))
                                            {
                                                Huong = "Left";
                                            }
                                        }
                                }
                                #endregion
                                break;
                            case "Left":
                                #region
                                if (Math.Abs(curPo.X - Index.Y * 128) <= 00)
                                {
                                    if (!GamePage.map.XacDinhRe(Index, Huong).Equals(Huong) && Index != IndexRe)
                                    {
                                        Huong = GamePage.map.XacDinhRe(Index, Huong);
                                        IndexRe = Index;
                                    }
                                    else
                                        if ((Index.Y == 0) || Map.matrix[(int)Index.X, (int)Index.Y - 1] != 0)
                                        {
                                            if (((int)Index.X > 0) && (Map.matrix[(int)Index.X - 1, (int)Index.Y] == 0))
                                            {
                                                Huong = "Top";
                                            }
                                            else if (((int)Index.X < Map.SizeOfMatrix.X - 1) && (Map.matrix[(int)Index.X + 1, (int)Index.Y] == 0))
                                            {
                                                Huong = "Bottom";
                                            }
                                        }
                                }
                                #endregion
                                break;
                            case "Right":
                                #region
                                if (!GamePage.map.XacDinhRe(Index, Huong).Equals(Huong) && Index != IndexRe)
                                {
                                    Huong = GamePage.map.XacDinhRe(Index, Huong);
                                    IndexRe = Index;
                                }
                                else
                                    if ((Index.Y == Map.SizeOfMatrix.Y - 1) || Map.matrix[(int)Index.X, (int)Index.Y + 1] != 0)
                                    {
                                        if (((int)Index.X > 0) && (Map.matrix[(int)Index.X - 1, (int)Index.Y] == 0))
                                        {
                                            Huong = "Top";
                                        }
                                        else if (((int)Index.X < Map.SizeOfMatrix.X - 1) && (Map.matrix[(int)Index.X + 1, (int)Index.Y] == 0))
                                        {
                                            Huong = "Bottom";
                                        }
                                    }
                                #endregion
                                break;
                            case "Bottom":
                                #region
                                if (!GamePage.map.XacDinhRe(Index, Huong).Equals(Huong) && Index != IndexRe)
                                {
                                    Huong = GamePage.map.XacDinhRe(Index, Huong);
                                    IndexRe = Index;
                                }
                                else
                                    if ((Index.X == Map.SizeOfMatrix.X - 1) || Map.matrix[(int)Index.X + 1, (int)Index.Y] != 0)
                                    {
                                        if (((int)Index.Y < Map.SizeOfMatrix.Y - 1) && (Map.matrix[(int)Index.X, (int)Index.Y + 1] == 0))
                                        {
                                            Huong = "Right";
                                        }
                                        else if (((int)Index.Y > 0) && (Map.matrix[(int)Index.X, (int)Index.Y - 1] == 0))
                                        {
                                            Huong = "Left";
                                        }
                                    }
                                #endregion
                                break;
                            default: break;
                        }
                    }
                    #endregion

                    #region di chuyển
                    if (isEnable && !Status.Equals("dying"))
                    {
                        #region thực hiện việc đi lại
                        if (Huong.Equals("Bottom"))
                        {
                            curPo.Y += (Speed - Speed * giamSpeed / 100) / 2;
                        }
                        else if (Huong.Equals("Left"))
                        {
                            curPo.X -= (Speed - Speed * giamSpeed / 100) / 2;
                        }
                        else if (Huong.Equals("Top"))
                        {
                            curPo.Y -= (Speed - Speed * giamSpeed / 100) / 2;
                        }
                        else
                        {
                            curPo.X += (Speed - Speed * giamSpeed / 100) / 2;
                        }
                        #endregion
                    }
                    #endregion
                    #endregion
                }
                #region tới nhà thì phá hoại
                if (curPo.X >= Map.SizeOfMatrix.Y * 128)
                {
                    isEnable = false;
                    phahoai.Play();
                    if (MainPage.isRung)
                        testVibrateController.Start(TimeSpan.FromSeconds(1));
                    GamePage.CurLife -= Dame;
                }
                #endregion
            }
        }
        public void miniDraw(SpriteBatch sprite, ContentManager cont, Vector2 sizeTotal, int Row, int Col)
        {
            sprite.Draw(Image, (curPo + DolechAnh) * GamePage.Scale + GamePage.Dolech, new Rectangle(((countDraw / 5) % Col) * (int)(sizeTotal.X / Col), ((countDraw / 5) / Col) * (int)(sizeTotal.Y / Row), (int)(sizeTotal.X / Col), (int)(sizeTotal.Y / Row)), isTonTaiHieuUng("Poison") ? (((countDraw / 6) % 2 == 0) ? Color.White * (isTangHinh ? 0.5f : 1.0f) : Color.Green * (isTangHinh ? 0.5f : 1.0f)) : Color.White * (isTangHinh ? 0.5f : 1.0f), 0f, Vector2.Zero, scale * GamePage.Scale, SpriteEffects.None, 0f);
            if (Status.Equals("dying") || (!isTonTaiHieuUng("Stun") && !isTonTaiHieuUng("Frozen")))
                countDraw++;
            if (countDraw >= Row * Col * 5)
            {
                if (Status.Equals("dying"))
                {
                    #region sau khi vẽ dying
                    if (isTonTaiHieuUng("Revival"))
                    {
                        curHP = HP * getCountEffect("Revival") / 100;
                        Status = "walking";
                    }
                    else
                        isEnable = false;
                    #endregion
                }
                else if (Status.Equals("walking") && Type % 5 == 0)
                {
                    if (counttoHarming == 4)
                    {
                        Status = "harming";
                        counttoHarming = 0;
                    }
                    else counttoHarming++;
                }
                else { Status = "walking"; isCanXe = false; }
                countDraw = 0;
            }
        }
        public void OptionDraw(SpriteBatch sprite, Vector2 position, Vector2 Dolech, float scale, bool isCD)
        {
            scale = 128 * scale / (sizeTotal.Y / Row);
            sprite.Draw(Image, position + Dolech, new Rectangle(((countDraw / 5) % Col) * (int)(sizeTotal.X / Col), ((countDraw / 5) / Col) * (int)(sizeTotal.Y / Row), (int)(sizeTotal.X / Col), (int)(sizeTotal.Y / Row)), Color.White * (isTangHinh ? 0.5f : 1.0f), 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
            if (isCD)
            {
                countDraw++;
                if (countDraw >= Row * Col * 5) countDraw = 0;
            }
        }

        public void setTruMau(float Physic, float Magic, string Effect, float countEffect, int countTimeEffect)
        {
            isNe = false;
            if (Status.Equals("walking") || Status.Equals("harming"))
            {
                #region nếu không có kháng hiệu ứng thì sẽ bị chịu hiệu ứng
                if (!Ablility.Equals("Khang"))
                {
                    if (!Effect.Equals("") && !Effect.Equals("None") && !Effect.Equals("Dame"))
                    {
                        AddListEffect(Effect, countEffect, countTimeEffect);
                    }
                }
                #endregion
                if (isTangHinh)
                {
                }
                else if (Ablility.Equals("Ne") && countNeAndTangHinh / 10 >= 7)
                {
                    isNe = true;
                }
                else
                {
                    isNe = false;
                    curHP -= (Physic - Physic * PhysicDef / 100) + (Magic - Magic * MagicDef / 100);
                    if (Effect.Equals("Dame")) curHP -= ((Physic - Physic * PhysicDef / 100) + (Magic - Magic * MagicDef / 100)) * 0.15f;
                    if (curHP <= 0)
                    {
                        Status = "dying";
                        if ((Ablility.Equals("HS30") || Ablility.Equals("HS50")) && !isRevival)
                        {
                            isRevival = true;
                            reveal.Play();
                            AddListEffect("Revival", Ablility.Equals("HS30") ? 30 : 50, 1);
                        }
                        else
                        {
                            die.Play();
                            GamePage.Money += Value;
                            GamePage.Score += Value * 2;
                            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[1])
                            {
                                GamePage.Money += Value / 20;
                            }
                        }
                        countDraw = 0;
                    }
                }
            }
        }

        private void AddListEffect(string Effect, float countEffect, int countTimeEffect)
        {
            int i;
            for (i = 0; i < effects.Count; i++)
            {
                if (effects[i].getNameEffect().Equals(Effect))
                {
                    if (effects[i].getValueEffect() <= countEffect)
                    {
                        effects.RemoveAt(i);
                        effects.Add(new MonsterEffect(Effect, (int)countEffect, (int)countTimeEffect));
                        break;
                    }
                }
            }
            if (i == effects.Count)
            {
                effects.Add(new MonsterEffect(Effect, (int)countEffect, (int)countTimeEffect));
            }
        }

        private bool isTonTaiHieuUng(string ef)
        {
            foreach (MonsterEffect effect in effects)
            {
                if (effect.getNameEffect().Equals(ef)) return true;
            }
            return false;
        }
        private int getCountEffect(string ef)
        {
            foreach (MonsterEffect effect in effects)
            {
                if (effect.getNameEffect().Equals(ef)) return effect.getValueEffect();
            }
            return 0;
        }
    }
}
